package org.example.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.example.domain.dto.DTO;
import org.example.domain.dto.ErpDepotItemDTO;
import org.example.domain.po.*;
import org.example.domain.po.ErpAccountItemDTO;
import org.example.service.ErpAccountHeadService;
import org.example.service.ErpAccountItemService;
import org.example.service.ErpRoleService;
import org.example.service.impl.ErpAccountHeadServiceImpl;
import org.example.service.impl.ErpAccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/accountHead")
public class ErpAccountHeadController {
    @Autowired
    private ErpAccountHeadServiceImpl accountHeadService;
    @Autowired
    private ErpAccountItemService accountItemService;

    @Autowired
    private ErpRoleService roleService;

    @GetMapping("/list")
    public R<Page<ErpAccountHead>>list(@RequestParam(defaultValue = "1") Integer current,
                                       @RequestParam(defaultValue = "3") Integer size,@Param("billNo")String billNo, @Param("billTime") String billTime, @Param("status") String status,
                                       @Param("organId") Long organId, @Param("handsPersonId") Long handsPersonId, @Param("accountId") Long accountId,
                                       @Param("supId") Long supId, @Param("remark") String remark,@Param("username") String username, @RequestAttribute ErpUser user){
        Page<ErpAccountHead> page = new Page<>(current, size);


        try {
            page = accountHeadService.selectByExample(page,billNo,billTime,status,organId,handsPersonId,accountId,supId,remark,username);
          
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/listzc")
    public R<Page<ErpAccountHead>>selectByExamplezc(@RequestParam(defaultValue = "1") Integer current,
                                       @RequestParam(defaultValue = "3") Integer size,@Param("billNo")String billNo, @Param("billTime") String billTime, @Param("status") String status,
                                       @Param("organId") Long organId, @Param("handsPersonId") Long handsPersonId, @Param("accountId") Long accountId,
                                       @Param("supId") Long supId, @Param("remark") String remark,@Param("username") String username,@Param("type")String type){
        Page<ErpAccountHead> page = new Page<>(current, size);
        try {
            page = accountHeadService.selectByExamplezc(page,billNo,billTime,status,organId,handsPersonId,accountId,supId,remark,username, type);
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }
    @GetMapping("/listsyfk")
    public R<Page<ErpAccountHead>>selectByExamplesyfk(@RequestParam(defaultValue = "1") Integer current,
                                                    @RequestParam(defaultValue = "3") Integer size,@Param("billNo")String billNo, @Param("billTime") String billTime, @Param("status") String status,
                                                    @Param("organId") Long organId, @Param("handsPersonId") Long handsPersonId, @Param("accountId") Long accountId,
                                                    @Param("supId") Long supId, @Param("remark") String remark,@Param("username") String username,@Param("type")String type){
        Page<ErpAccountHead> page = new Page<>(current, size);
        try {
            page = accountHeadService.selectByExamplesyfk(page,billNo,billTime,status,organId,handsPersonId,accountId,supId,remark,username, type);
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }
    @GetMapping("/listzz")
    public R<Page<ErpAccountHead>>selectByExamplezz(@RequestParam(defaultValue = "1") Integer current,
                                                    @RequestParam(defaultValue = "3") Integer size,@Param("billNo")String billNo, @Param("billTime") String billTime, @Param("status") String status,
                                                    @Param("organId") Long organId, @Param("handsPersonId") Long handsPersonId, @Param("accountId") Long accountId,
                                                    @Param("supId") Long supId, @Param("remark") String remark,@Param("username") String username,@Param("type")String type){
        Page<ErpAccountHead> page = new Page<>(current, size);
        try {
            page = accountHeadService.selectByExamplezz(page,billNo,billTime,status,organId,handsPersonId,accountId,supId,remark,username, type);
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }
    @GetMapping("/listsk")
    public R<Page<ErpAccountHead>>selectByExamplesk(@RequestParam(defaultValue = "1") Integer current,
                                                    @RequestParam(defaultValue = "3") Integer size,@Param("billNo")String billNo, @Param("billTime") String billTime, @Param("status") String status,
                                                    @Param("organId") Long organId, @Param("handsPersonId") Long handsPersonId, @Param("accountId") Long accountId,
                                                    @Param("supId") Long supId, @Param("remark") String remark,@Param("username") String username,@Param("supName") String supName,@Param("type") String type){
        Page<ErpAccountHead> page = new Page<>(current, size);
        try {
            page = accountHeadService.selectByExamplesk(page,billNo,billTime,status,organId,handsPersonId,accountId,supId,remark,username,supName,type);
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }
    @PutMapping("/updatestatus")
    public R updatestatus(@RequestBody UpdateStatusRequest request) {
        if (request.getIds() == null || request.getIds().isEmpty()) {
            return R.fail("ID 列表不能为空");
        }

        try {
//            erpDepotHeadService.updateOrderStatus(request.getIds(), request.getStatus());
            boolean update = accountHeadService.update(request.getIds(), request.getStatus());
            return R.success();
        } catch (Exception e) {
            return R.fail("批量更新失败：" + e.getMessage());
        }
    }
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        accountHeadService.removeById(id);
        return R.success();
    }

    //    批量删除
    @DeleteMapping("/delete")
    public R deleteBatch(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            accountHeadService.removeById(id);
        }
        return R.success();
    }
    @GetMapping("/findRemoven")
    public R<List<ErpAccountHead>> findRemoven(Long id) {
        List<ErpAccountHead> list = accountHeadService.selectBy(id);
        return R.success(list);
    }
    @GetMapping("/findExpendituresfind")
    public R<List<ErpAccountHead>> findRemovenzc(String type,Long id) {
        List<ErpAccountHead> list = accountHeadService.selectByzc(type,id);
        return R.success(list);
    }
    @GetMapping("/findExpendituresfindzz")
    public R<List<ErpAccountHead>> findRemovenzz(String type,Long id) {
        List<ErpAccountHead> list = accountHeadService.selectByzz(type,id);
        return R.success(list);
    }
    @GetMapping("/findReceivables")
    public R<List<ErpAccountHead>> findRemovensk(String type,Long id) {
        List<ErpAccountHead> list = accountHeadService.selectBysk(type,id);
        return R.success(list);
    }
    @GetMapping("/findReceivablessyfk")
    public R<List<ErpAccountHead>> findRemovensk(Long id) {
        List<ErpAccountHead> list = accountHeadService.selectBysyfk(id);
        return R.success(list);
    }
    @PostMapping("/insertFind")
    public R add(@RequestBody DepotRequestFind depotRequest, @RequestAttribute ErpUser user) {
        ErpAccountHead erpAccountHead = depotRequest.getErpAccountHead();
        List<ErpAccountItem> erpDepotItems = depotRequest.getErpAccountItems();
        erpAccountHead.setTenantId(user.getTenantId());
        erpAccountHead.setBillTime(new Date());

        boolean insert = accountHeadService.save(erpAccountHead);

        if (insert) {
            Long headerId = erpAccountHead.getId();

            for (ErpAccountItem item : erpDepotItems) {
                item.setHeaderId(headerId);
                ErpAccountItemDTO erpDepotItem = BeanUtil.copyProperties(item, ErpAccountItemDTO.class);
                accountItemService.insert(erpDepotItem);
            }

        }

        return R.success();
    }
    @PutMapping("/update")
    public R update(@RequestBody DepotRequestFind depotRequest, @RequestParam String billNo) {
        try {
            // 获取头部和明细信息
            ErpAccountHead erpDepotHead = depotRequest.getErpAccountHead();
            List<ErpAccountItem> erpAccountItems = depotRequest.getErpAccountItems();
            erpDepotHead.setBillTime(new Date());
            int headUpdateCount = accountHeadService.updateby(erpDepotHead, billNo);
            if (headUpdateCount > 0) {
                Long headerId = accountHeadService.lookByHeaderId(billNo);

                for (ErpAccountItem item : erpAccountItems) {
                    boolean itemUpdateCount = accountItemService.updateById(item);

                    if (!itemUpdateCount) {
                        return R.fail("子表更新失败");
                    }
                }
            } else {
                return R.fail("头部更新失败");
            }

            return R.success("更新成功");
        } catch (Exception e) {
            return R.fail("处理异常: " + e.getMessage());
        }
    }


}
